namespace ClassyBlog.Web.Specs.Unit
{
    using System;
    using System.Dynamic;

    using Machine.Specifications;
    using NSubstitute;

    using DomainObjects;
    using Infrastructure;

    [Subject(typeof(ArticleViewHelper))]
    public class when_trying_to_set_view_properties_of_an_null_instance
    {
        static IViewBagContainer view;
        static Exception exception;

        Establish context = () => view = null;

        Because of = () => exception =
            Catch.Exception(() =>
                view.SetViewPropertiesFrom(ObjectMother.CreatePage()));

        It should_throw_exception = () =>
            exception.ShouldBeOfType<ArgumentNullException>();
    }

    [Subject(typeof(ArticleViewHelper))]
    public class when_trying_to_set_view_properties_with_null_article
    {
        static IViewBagContainer view;
        static Exception exception;

        Establish context = () => view = Substitute.For<IViewBagContainer>();

        Because of = () => exception =
            Catch.Exception(() => view.SetViewPropertiesFrom((IArticle)null));

        It should_throw_exception = () =>
            exception.ShouldBeOfType<ArgumentNullException>();
    }

    [Subject(typeof(ArticleViewHelper))]
    public class when_view_properties_are_set_from_article
    {
        static IViewBagContainer view;
        static IArticle article;

        Establish context = () =>
        {
            view = new ViewBagContainer();
            article = ObjectMother.CreatePost();
        };

        Because of = () => view.SetViewPropertiesFrom(article);

        It should_have_same_title = () =>
            ((string)view.ViewBag.Title).ShouldEqual(article.Title);

        It should_have_meta_keywords = () =>
            ((string)view.ViewBag.MetaKeywords).
            ShouldEqual(article.MetaKeywords);

        It should_have_meta_description = () =>
            ((string)view.ViewBag.MetaDescription).
            ShouldEqual(article.MetaDescription);

        It should_have_meta_author = () =>
            ((string)view.ViewBag.Author).ShouldEqual(article.Author);

        private class ViewBagContainer : IViewBagContainer
        {
            public ViewBagContainer()
            {
                ViewBag = new ExpandoObject();
            }

            public dynamic ViewBag { get; private set; }
        }
    }
}